//
//  CTCStatisticsDetailVC.m
//  ConsultantVersion
//
//  Created by 小龙 on 2017/5/9.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCStatisticsDetailVC.h"
#import "CTCStatisticsDetailCell.h"
#import "CTCStatisticsModel.h"

@interface CTCStatisticsDetailVC ()
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSUInteger lastID;
@end

@implementation CTCStatisticsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self setupNavBar];
    
    [self setupTableView];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CTCStatisticsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CTCStatisticsDetailCell" forIndexPath:indexPath];
    cell.statisticsModel = self.dataArr[indexPath.section];
    return cell;
}


- (void)setupNavBar
{
    if (self.statisticsType == CTCStatisticsTypeRepaid) {
        self.title = @"快速咨询";
    }else {
        self.title = @"线上咨询";
    }
    
}


- (void)setupTableView
{
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(11-35, 0, 0, 0);
    
    self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)loadNewData
{
    _lastID = 1;
    [self.dataArr removeAllObjects];
    [self.tableView.mj_footer resetNoMoreData];
    [self loadData];
}

- (void)loadMoreData
{
    [self loadData];
}


- (void)loadData
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pageIndex"] = @(self.lastID);
    parameters[@"pageEnd"] = @(self.lastID+9);
    if (self.statisticsType == CTCStatisticsTypeRepaid) {
        parameters[@"type"] = @"1";
    } else {
        parameters[@"type"] = @"2";
    }
    
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kStatisticsUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"%@",responseObject);
        [MBProgressHUD hideHUD];
        [self stopRefrsh];
        CTCStatisticsListModel *model = [CTCStatisticsListModel yy_modelWithJSON:responseObject];
        if ([model.code integerValue] == 1) {
            
            if (model.dataArr.count) {
                [self.dataArr addObjectsFromArray:model.dataArr];
                self.lastID += model.dataArr.count;
                [self.tableView reloadData];
            }
        } else {
            [MBProgressHUD showErrorMessage:model.des];
            if ([model.code integerValue]== 2) {
                CTCLoginViewController *vc = [CTCLoginViewController viewControllerFromStoryboard];
                MPWeakSelf(self);
                vc.loginSuccess = ^(){
                    [weakself loadNewData];
                };
                [self presentViewController:vc animated:YES completion:nil];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"%@",error);
        [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        [self stopRefrsh];
    }];
}


- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

- (NSUInteger)lastID
{
    if (_lastID == 0) {
        _lastID = 1;
    }
    
    return  _lastID;
}


- (void)stopRefrsh
{
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

@end
